package com.milk;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.logging.Logger;

import javax.sql.DataSource;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import com.lorne.tx.bean.TxTransactionLocal;
import com.lorne.tx.compensate.repository.CompensateDataSource;
import com.lorne.tx.db.AbstractResourceProxy;
import com.lorne.tx.db.LCNDataSourceProxy;
import com.lorne.tx.db.relational.AbstractDBConnection;
import com.milk.properties.MyProperties;

@Configuration
public class AppConfig implements InitializingBean{
	
	@Autowired
	private MyProperties myProperties;

	/**
	 * 
	 */
	@Autowired
	@Qualifier("dataSource")
	private DataSource primaryDataSource;
	/**
	 * 补偿操作的数据源
	 */
	@Autowired
	@Qualifier("compensateDruidDataSource")
	private DataSource secondaryDataSource;
	/**
	 * 代理原数据源连接池的代理连接池对象 (主数据源)
	 */
	@Autowired
	@Qualifier("lcnDataSourceProxy")
	private DataSource lcnDataSourceProxy;
	
	
	@Bean(name = "dataSource")
    @ConfigurationProperties(prefix="spring.datasource")
    public DataSource primaryDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "compensateDruidDataSource")
    @ConfigurationProperties(prefix="spring.compensateDruidDataSource")
    public DataSource secondaryDataSource() {
        return DataSourceBuilder.create().build();
    }
    
    /**
     * lcn事务框架中的补偿bean
     * @return
     */
    @Bean(name="compensateDataSource")
    public CompensateDataSource initCompensateDataSource(){
    	CompensateDataSource dataSource = new CompensateDataSource();
    	dataSource.setDataSource(secondaryDataSource);
		return dataSource;
    }
    
    /**
     * <!--lcn代理连接池配置-->
     */
    @Bean(name="lcnDataSourceProxy")
    @Primary
    public DataSource initLCNDataSourceProxy(){
    	LCNDataSourceProxy lcnDataSourceProxy = new LCNDataSourceProxy();
    	lcnDataSourceProxy.setDataSource(null);
    	lcnDataSourceProxy.setMaxCount(myProperties.getMaxActive());
    	return lcnDataSourceProxy;
    }
    
    /**
     * 事务管理器
     * @return
     */
    @Bean("transactionManager")
    public DataSourceTransactionManager initDataSourceTransactionManager(){
    	DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
    	transactionManager.setDataSource(primaryDataSource);
    	return transactionManager;
    }

	public void afterPropertiesSet() throws Exception {
		// TODO Auto-generated method stub
		//给代理数据源添加真实数据源
		((LCNDataSourceProxy)lcnDataSourceProxy).setDataSource(primaryDataSource);
	}
}
